<template>
  <dv-full-screen-container class="page-border">
    <!-- 动态粒子背景 -->
    <Particles
        id="tsparticles"
        :options="particlesOptions"
        class="particles-background"
    />

    <!-- 顶部和底部的装饰线条 -->
    <dv-decoration-8
        class="decoration-top"
        :color="['#4fd2dd', '#4fd2dd']"
        :dur="3"
    />
    <dv-decoration-8
        class="decoration-bottom"
        :color="['#4fd2dd', '#4fd2dd']"
        :dur="3"
        reverse
    />

    <!-- 登录框 -->
    <dv-border-box-12 class="login-box" :color="['#4fd2dd', '#4fd2dd']" :dur="3">
      <h1>农业大棚数字化追溯平台</h1>
      <form @submit.prevent="handleLogin">
        <div class="input-group">
          <el-input
              v-model="username"
              placeholder="请输入用户名"
              prefix-icon="el-icon-user"
          />
        </div>
        <div class="input-group">
          <el-input
              v-model="password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
          />
        </div>
        <el-button
            type="primary"
            native-type="submit"
            class="login-button"
            :loading="loading"
        >
          立即登录
        </el-button>
      </form>
    </dv-border-box-12>
  </dv-full-screen-container>
</template>

<script>
import { ElMessage } from "element-plus";
import { loginUser } from "@/api/Login";

export default {
  data() {
    return {
      username: "",
      password: "",
      loading: false,
      particlesOptions: {
        background: {
          color: { value: "#1c2833" },
        },
        fpsLimit: 60,
        interactivity: {
          detectsOn: "canvas",
          events: {
            onClick: { enable: true, mode: "push" },
            onHover: { enable: true, mode: "grab" },
            resize: true,
          },
          modes: {
            push: { quantity: 6 },
            grab: { distance: 150, line_linked: { opacity: 0.7 } },
          },
        },
        particles: {
          color: { value: "#4fd2dd" },
          links: {
            color: "#4fd2dd",
            distance: 120,
            enable: true,
            opacity: 0.5,
            width: 1.5,
          },
          move: {
            direction: "none",
            enable: true,
            outMode: "out",
            random: true,
            speed: 2.5,
          },
          number: {
            density: { enable: true, area: 800 },
            value: 100,
          },
          opacity: { value: 0.7 },
          shape: { type: "circle" },
          size: { random: true, value: 4 },
        },
        detectRetina: true,
      },
    };
  },
  methods: {
    async handleLogin() {
      if (!this.username || !this.password) {
        ElMessage.warning("用户名和密码不能为空");
        return;
      }

      this.loading = true;

      try {
        const response = await loginUser(this.username, this.password);

        if (response.token) {
          ElMessage.success("登录成功");
          sessionStorage.setItem("access_token", response.token);
          this.$router.push("/Index");
        } else {
          ElMessage.error("登录失败，请检查用户名和密码");
        }
      } catch (error) {
        console.error("登录失败:", error);
        ElMessage.error("登录失败，请稍后重试");
      } finally {
        this.loading = false;
      }
    },
  },
};
</script>

<style scoped>
.page-border {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #1c2833, #308aff);
  position: relative;
  overflow: hidden;
}

.particles-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1; /* 背景置于底层 */
}

.decoration-top,
.decoration-bottom {
  position: absolute;
  width: 100%;
  height: 10%;
  z-index: 2; /* 装饰线条置于中间层 */
}

.decoration-top {
  top: 0;
  left: 0;
}

.decoration-bottom {
  bottom: 0;
  left: 0;
}

.login-box {
  background: rgba(255, 255, 255, 0.1);
  padding: 2rem;
  border-radius: 12px;
  box-shadow: 0 0 30px rgba(0, 0, 0, 0.7);
  text-align: center;
  width: 400px; /* 固定宽度 */
  height: auto; /* 高度自适应内容 */
  min-height: 400px; /* 最小高度 */
  z-index: 3; /* 登录框置于最上层 */
  animation: fadeIn 1s ease-in-out;
  font-size: 16px;
  color: #fff;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

@keyframes fadeIn {
  0% {
    opacity: 0;
    transform: translateY(-30px);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
  }
}

h1 {
  margin-bottom: 1.5rem;
  font-size: 1.75rem;
  color: #4fd2dd;
  animation: textGlow 2s infinite alternate;
}

@keyframes textGlow {
  0% {
    text-shadow: 0 0 10px #4fd2dd, 0 0 20px #4fd2dd;
  }
  100% {
    text-shadow: 0 0 20px #4fd2dd, 0 0 30px #4fd2dd;
  }
}

.input-group {
  margin-bottom: 1.2rem;
}

.login-button {
  width: 100%;
  margin-top: 1rem;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(1);
    background-color: #4fd2dd;
  }
  50% {
    transform: scale(1.05);
    background-color: #38b3c4;
  }
  100% {
    transform: scale(1);
    background-color: #4fd2dd;
  }
}

/* 响应式设计 */
@media (max-width: 400px) {
  .login-box {
    width: 90%;
    padding: 1.5rem;
    min-height: 350px; /* 小屏幕下调整最小高度 */
  }

  h1 {
    font-size: 1.5rem;
  }
}
</style>
